package crazyboyfeng.autodroid.tasks;

import java.util.ArrayList;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import crazyboyfeng.autodroid.bean.TaskBean;
import crazyboyfeng.autodroid.db.DBManager;
import crazyboyfeng.autodroid.service.AlarmReceiver;
import crazyboyfeng.autodroid.utils.DateTools;
import crazyboyfeng.autodroid.utils.GlobalContext;
import crazyboyfeng.autodroid.utils.WeekTools;

public class TaskManager {

	private DBManager mDB = DBManager.getInstance();
	public ArrayList<TaskBean> getAllTaskList(){
		return mDB.getAllTask();
	}
	
	public TaskBean getTaskById(String taskID){
		return mDB.getTask(taskID);
	}
	
	public void resetTaskAlarm(TaskBean task){
		if(task.isActive()){
		      //注销提醒
			setTaskActive(task.getId(), false);
		      //重新注册提醒
			setTaskActive(task.getId(), true);
		}
	}
	
	public void addNewTask(){
    	TaskBean task = new TaskBean();
    	task.setName("新建任务");
    	task.setDate("");
    	task.setTime("");
    	task.setType("1 2 3 4 5 600 700");
    	task.setTask("");
    	task.setState("Y");
    	task.setActive(false);
    	mDB.addTask(task);
	}
	public void addTask(TaskBean task){
    	mDB.addTask(task);
	}
	
	public void editTaskActive(String taskId,Boolean active){
		mDB.setTaskActive(taskId, active);
	}
	public void editTask(TaskBean task,TaskBean newTask){
		 if(task!=null){
			  newTask.setId(task.getId());
			  mDB.editTask(newTask);
	      }else{
	    	  mDB.addTask(task);
	      }
	}
	
	public void deleteTask(TaskBean task){
		if(task.isActive()){
			//判断是否活动状态，活动的话就注销Alarm
			setTaskActive(task.getId(),false);
		}
		mDB.delTask(task.getId());
	}
	
	//重新注册所有活动的task
	public void resetAllTaskActive(){
		ArrayList<TaskBean> list = mDB.getAllTask();
		for(int i=0;i<list.size();i++){
			TaskBean task = list.get(i);
			if(task.isActive()){
				registerAlarm(task);
			}
		}
	}
	
	//检查task是否在有效星期内
	public boolean checkTaskTime(TaskBean task){
		int now_week = WeekTools.getNowWeek();
		if(task.getType()!=""){
			String[] week = task.getType().split(" ");
			for(int i=0;i<week.length;i++){
				if(week[i].equals(now_week+"")){
					return true;
				}
			}
		}
		
		return false;
	}
	
	public void setTaskActive(String taskId,boolean enable){
		
		TaskBean task = mDB.getTask(taskId);
		if(enable){
			registerAlarm(task);
		}else{
			unregisterAlarm(task);
		}
		
	}
	
	/**
	 * register a Alarm
	 */
	private void registerAlarm(TaskBean task){
		AlarmManager alarmMgr = (AlarmManager) GlobalContext.getInstance().getSystemService(Context.ALARM_SERVICE);  
    	Intent intent = new Intent(GlobalContext.getInstance(), AlarmReceiver.class);  
    	int requestCode = Integer.parseInt(task.getId());    
    	intent.putExtra("taskId", task.getId());
    	PendingIntent pendIntent = PendingIntent.getBroadcast(GlobalContext.getInstance(),  
    	        requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);  

    	long taskTime = DateTools.getTaskTime(task.getTime());
    	alarmMgr.setRepeating(AlarmManager.RTC, taskTime,AlarmManager.INTERVAL_DAY, pendIntent); 
	}
	
	/**
	 * cancel a Alarm
	 */
	private void unregisterAlarm(TaskBean task){
		AlarmManager alarmMgr = (AlarmManager) GlobalContext.getInstance().getSystemService(Context.ALARM_SERVICE);  
    	Intent intent = new Intent(GlobalContext.getInstance(), AlarmReceiver.class);  
    	int requestCode = Integer.parseInt(task.getId());  
    	PendingIntent pendIntent = PendingIntent.getBroadcast(GlobalContext.getInstance(),  
    	        requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);  
    	alarmMgr.cancel(pendIntent);
	}
}
